package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.MonitorModuleApplication;
import com.af.monitorModule.common.MonitorTypeEnum;
import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Monitor;
import com.af.monitorModule.generator.entity.TimedTask;
import com.af.monitorModule.generator.entity.User;
import com.af.monitorModule.generator.entity.VO.MonitorVO;
import com.af.monitorModule.generator.mapper.MonitorMapper;
import com.af.monitorModule.generator.service.MonitorService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-02-23
 */
@Service
public class MonitorServiceImpl extends ServiceImpl<MonitorMapper, Monitor> implements MonitorService {

    private static final Logger logger = LoggerFactory.getLogger(MonitorModuleApplication.class);

    @Autowired
    private MonitorMapper monitorMapper;

    @Override
    public JSONObject findMonitor(String page, String limit, String search) {
        JSONObject condition = JSONObject.parseObject(search);
        Page<Monitor> p = new Page<Monitor>(Integer.parseInt(page), Integer.parseInt(limit));
        QueryWrapper<Monitor> wrapper = new QueryWrapper<>();
        if (condition != null)
            condition.forEach((k,v)->{
                if (!"".equals(v.toString().trim())){
                    if (k.contains("name")){
                        wrapper.like(k,v.toString());
                    }else {
                        wrapper.eq(k,v.toString());
                    }

                }
            });
        IPage<MonitorVO> monitor = monitorMapper.findMonitor(p, wrapper);
        JSONObject res = new JSONObject();
        res.put("data",monitor.getRecords());
        res.put("code",0);
        res.put("count",monitor.getTotal());
        res.put("msg","用户列表");
        return res;
    }

    @Override
    public JSONObject changeState(String id) {
        Monitor monitor = monitorMapper.selectById(id);
        if (monitor == null){
            return ResultEnum.DATA_ISNOT_EXIST.getResult();
        }
        monitor.setState(monitor.getState() == 0 ? 1: 0);
        monitorMapper.updateById(monitor);
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject batchStop(JSONObject ids) {
        if (ids == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        JSONArray ids1 = ids.getJSONArray("ids");
        StringBuffer stringBuffer = new StringBuffer();
        ids1.forEach((v)->{
            stringBuffer.append(","+v);
        });
        monitorMapper.stopBatch("(" + stringBuffer.toString().substring(1) + ")");
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject updateMonitor(Monitor monitor, String id) {
        if (monitor == null || id == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        monitor.setId(Integer.valueOf(id));
        int i = monitorMapper.updateById(monitor);
        return i == 1 ? ResultEnum.OK.getResult() : ResultEnum.VERSION_NO_CORRESPONDING.getResult();
    }

    @Override
    public JSONObject add(Monitor monitor) {
        if (monitor == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        monitor.setState(1);
        if (monitorMapper.insert(monitor) > 0){
            return ResultEnum.OK.getResult();
        }else {
            return ResultEnum.CREATE_ERROR.getResult();
        }

    }

    @Override
    public JSONObject getAllMonitorName() {
        JSONObject result = ResultEnum.OK.getResult();
        result.put("names", MonitorTypeEnum.getAllMonitorName());
        return result;
    }

    @Override
    public JSONObject switchAdd(JSONObject monitors) {
        Integer companyId = monitors.getInteger("companyId");
        monitors.remove("companyId");
        logger.info("批量增加脚本：" +monitors.toJSONString());
        monitors.forEach( (k,v) ->{
            Monitor monitor = new Monitor(MonitorTypeEnum.getOneById(Integer.parseInt(v.toString()))).setCompanyId(companyId).setState(1);
            monitorMapper.insert(monitor);
        });
        return ResultEnum.OK.getResult();
    }
}
